package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"shop_ppl/order_srv/appconfig"
	"sync"
)

var (
	err  error
	Db   *gorm.DB
	once sync.Once
)

func MysqlInit() {
	stu := appconfig.AppCon.InitMysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", stu.User, stu.Pass, stu.Hort, stu.Part, stu.Database)
	once.Do(func() {
		Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	})
	if err != nil {
		fmt.Println("mysql 连接失败", err)
		return
	}
	fmt.Println("mysql 连接成功")
	Db.AutoMigrate(new(Goods))
}
